﻿var vm = this.lookupController().getViewModel();

var win = Ext.create('Ext.window.Window', {
    title: 'Choose Agency',
    height: 600,
    width: 900,
    modal: true,
    autoShow: true,
    layout: 'fit',
    buttons: [{
        xtype: "button",
        text: "Comfirm",
        handler: function () {
            var win = this.up("window"),
                refs = win.getReferences();
            //赋值
            var record = refs.datagrid1.getSelection();
            var id = record[0].id;
            Ext.PAIRest.request({
                ns: PAI.application.url,
                url: 'last/hrm_agencyBoarding/page',
                method: "post",
                async: false,
                data: {
                    id: id
                },
                success: function (record) {
                    if (record.hrm_agencyBoardingList.length > 0) {
                        vm.set('NebulogyForm.companyResourcesRequest.companyResources', {
                            accessCard: 0,
                            cdsid: 0,
                            jlrFamily: 0,
                            locker: 0,
                            none_: 0,
                            skypeAccount: 0
                        });
                        record = record.hrm_agencyBoardingList[0];
                        var agencyInfo = {};
                        var approvalInfo = {};
                        //第三方人员信息
                        agencyInfo.choosesAgency = record.agencyName;
                        agencyInfo.serviceType = record.serviceType;
                        agencyInfo.agencyName = record.agencyName;
                        agencyInfo.nameEnglish = record.nameEnglish;
                        agencyInfo.nameFirst = record.nameFirst;
                        agencyInfo.surname = record.surname;
                        agencyInfo.gender = record.gender;
                        agencyInfo.mobilePhone = record.mobilePhone;
                        agencyInfo.agencyDepartment = record.agencyDepartment;
                        agencyInfo.costCenter = record.costCenter;
                        agencyInfo.serviceCompany = record.serviceCompany;
                        agencyInfo.agencyPosition = record.agencyPosition;
                        agencyInfo.reportTo = record.reportTo;
                        agencyInfo.idNoOrPassportNo = record.idNoOrPassportNo;
                        //agencyInfo.idType = record.idType;
                        agencyInfo.serviceStart = record.serviceStart;
                        agencyInfo.serviceEnd = record.serviceEnd;
                        agencyInfo.prs = record.prs;
                        agencyInfo.prsAmounts = record.prsAmounts;
                        agencyInfo.pos = record.pos;
                        agencyInfo.posAmounts = record.posAmounts;
                        agencyInfo.workLocation = {};
                        //摘要赋值
                        vm.set('NebulogyForm.abstractName', Ext.String.format('Agency {0} Agency Staff Change Boarding Process', record.agencyName));
                        //清空其他数据
                        if (ref.departmentHead.getValue() != "") {
                            ref.departmentHead.setValue()
                        };
                        if (ref.cardStartDate.getValue() != "") {
                            ref.cardStartDate.setValue()
                        };
                        if (ref.cardEndDate.getValue() != "") {
                            ref.cardEndDate.setValue()
                        };
                        if (ref.comments.getValue() != "") {
                            ref.comments.setValue()
                        };
                        if (ref.file1.getValue() != "") {
                            ref.file1.store.removeAll();
                        };
                        //判断serviceType为JV Partner
                        if (record.serviceType == '4') {
                            ref.prs.setDisabled(true);
                            ref.prsAmounts.setDisabled(true);
                            ref.pos.setDisabled(true);
                            ref.posAmounts.setDisabled(true);
                        } else {
                            ref.prs.setDisabled(false);
                            ref.prs.setAllowBlank(false);
                            ref.prsAmounts.setDisabled(false);
                            ref.prsAmounts.setAllowBlank(false);
                            ref.pos.setDisabled(false);
                            ref.pos.setAllowBlank(false);
                            ref.posAmounts.setDisabled(false);
                            ref.posAmounts.setAllowBlank(false);
                        }
                        vm.set('NebulogyForm.agencyInfo', agencyInfo);
                        vm.set('NebulogyForm.approvalInfo', approvalInfo);
                        vm.set('NebulogyForm.agencyBoardingId', record.id);
                        vm.set('NebulogyForm.personalID', record.personalID);
                        vm.set('NebulogyForm.bizModelId', record.formDataId);

                        vm.set('NebulogyForm.idType', record.idType);
                        vm.set('NebulogyForm.file1', record.file1);


                        //多选控件需要另外做处理.
                        var jlrOfficeArr = (record.jlrOfficeStr || '').split(',');
                        var wareHouseArr = (record.wareHouseStr || '').split(',');
                        var newBundFloorsArr = (record.newBundFloorsStr || '').split(',');

                        //保存最原始的newBundFloor 数据
                        vm.set('NebulogyForm.originalNewBundFloors', newBundFloorsArr);

                        Ext.each(ref.workLocation.items.items, function (field) {
                            field.setValue(false);
                            if (Ext.Array.contains(jlrOfficeArr, field.boxLabel)) {
                                field.setValue(true);
                            }
                            if (Ext.Array.contains(wareHouseArr, field.boxLabel)) {
                                field.setValue(true);
                            }
                        });
                        if (jlrOfficeArr.indexOf('Shanghai New Bund Office') != -1) {
                            Ext.each(ref.newBundFloors.items.items, function (field) {
                                field.setValue(false);
                                if (Ext.Array.contains(newBundFloorsArr, field.boxLabel)) {
                                    field.setValue(true);
                                    ref.newBundFloors.setAllowBlank(true);
                                    ref.newBundFloors.setDisabled(true);
                                }
                            });
                        }

                        ref.gender.setValue(agencyInfo.gender);
                        ref.serviceCompany.setValue(agencyInfo.serviceCompany);
                    }
                }
            });

            win.close();
        }
    }, {
        xtype: "button",
        text: "Cancel",
        handler: function () {
            this.up("window").close();
        }
    }],
    //主视图内部控件
    referenceHolder: true,
    items: [{
        xtype: "paigrid",
        searchText: 'Agency Name',
        mobileDisplay: true,
        hidden: false,
        readOnly: false,
        readOnlyHead: true,
        name: "datagrid1",
        editable: false,
        enableRowStatus: false,
        enableAdd: false,
        enableActions: true,
        reference: "datagrid1",
        enableSearch: true,
        enableDel: false,
        enablePaging: true,
        pagingConfigs: {
            displayInfo: true
        },
        columns: [{
            text: language("Service Type"),
            dataIndex: "serviceType",
            minWidth: 100,
            flex: 1,
            renderer: function (value) {
                var str = '';
                switch (value) {
                    case '1': str = 'Short-term Project'; break;
                    case '2': str = 'Long-term Outsourcing Service'; break;
                    case '3': str = 'Short-term Replacement for LOA Employee'; break;
                    case '4': str = 'JV Partner'; break;
                    case '5': str = 'Guest'; break;
                    case '6': str = 'Intern'; break;
                }
                return str;
            }
        }, {
            text: language("Agency Name"),
            dataIndex: "agencyName",
            minWidth: 100,
            flex: 1
        }, {
            text: language("CDSID"),
            dataIndex: "approverCDSID",
            minWidth: 100,
            flex: 1
        }],

        //设置模糊查询字段
        defineSearchFilter: function (searchfield, value) {
            var me = this,
                items = [{
                    property: 'agencyName',
                    anyMatch: true,
                    value: value
                }]

            return items;
        },
        store: {
            type: 'store',
            autoLoad: true,
            remoteFilter: true,
            pageSize: 10,
            fields: [
                { name: "serviceType" },
                { name: "agencyName" },
                { name: "approverCDSID" },
                { name: "formDataId" }
            ],
            proxy: {
                type: 'pai.rest',
                ns: PAI.application.url,
                requestName: 'customQuery',
                url: "last/hrm_agencyBoarding",
                extraParams: {
                    fields: [
                        { name: "serviceType" },
                        { name: "agencyName" },
                        { name: "approverCDSID" },
                        { name: "formDataId" },
                        { name: "id" }
                    ],
                    filter: 'pushStatus=1'
                },
                reader: {
                    rootProperty: "data"
                }
            }
        }
    }],
    listeners: {
        close: function () {
            Ext.getBody().unmask();
        }
    }
}).show();